/**
 * 
 */
package com.baijia.tianxiao.push.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.baijia.tianxiao.push.model.PushBindRecord;
import com.baijia.tianxiao.push.result.BaseApiResult;
import com.baijia.tianxiao.push.service.PushBindRecordService;

/**
 * @author leiruiqi
 *
 */
@RequestMapping("/pushBind")
@RestController
public class PushBindController {

	@Resource
	private PushBindRecordService pushBindRecordService;
	
	@RequestMapping("/bind")
	public BaseApiResult bind(String pushBindRecordStr){
		BaseApiResult apiResult = new BaseApiResult();
		
		PushBindRecord pushBindRecord = jsonStr2PushBindRecord(pushBindRecordStr);
		if(pushBindRecord == null ){
			apiResult.setCode("0");
			apiResult.setMsg("params Invalid");
		}
		
		try {
			pushBindRecordService.saveOfUpdateForBind(pushBindRecord);
			apiResult.setCode("1");
			return apiResult;
		} catch (Exception e) {
			apiResult.setCode("0");
			apiResult.setMsg("exception");
			return apiResult;
		}
	}
	
	private PushBindRecord jsonStr2PushBindRecord(String str){
		PushBindRecord record = null;
		try {
			record = JSON.parseObject(str, PushBindRecord.class);
		} catch (Exception e) {
			
		}
		return record;
	}
	@RequestMapping("/unbind")
	public BaseApiResult unbind(String pushBindRecordStr){
		BaseApiResult apiResult = new BaseApiResult();
		PushBindRecord pushBindRecord = jsonStr2PushBindRecord(pushBindRecordStr);
		if(pushBindRecord == null ){
			apiResult.setCode("0");
			apiResult.setMsg("params Invalid");
		}
		try {
			pushBindRecordService.unBindByDeviceId(pushBindRecord.getDeviceId());
			apiResult.setCode("1");
			return apiResult;
		} catch (Exception e) {
			apiResult.setCode("0");
			apiResult.setMsg("exception");
			return apiResult;
		}
	}
	
	@RequestMapping("/queryUserBindStatus")
	public BaseApiResult queryUserBindStatus(String bizType,String userId){
		BaseApiResult apiResult = new BaseApiResult();
		PushBindRecord pushBindRecord = new PushBindRecord();
		pushBindRecord.setUserId(userId);
		pushBindRecord.setUserType(bizType);
		/*PushBindRecord pushBindRecord = jsonStr2PushBindRecord(pushBindRecordStr);
		if(pushBindRecord == null ){
			apiResult.setCode("0");
			apiResult.setMsg("params Invalid");
		}*/
		try {
			
			List<PushBindRecord> bindRecordList = pushBindRecordService.qureyUserBind(pushBindRecord.getUserId(),pushBindRecord.getUserType());
	
			apiResult.setCode("1");
			apiResult.setData(bindRecordList);
			return apiResult;
			
		} catch (Exception e) {
			
			apiResult.setCode("0");
			apiResult.setMsg("exception");
			
			return apiResult;
		}
	}
	
	@RequestMapping("/queryBindStatusByPrefix")
	public BaseApiResult queryUserBindStatusByPrefix(String bizType,String userId){
		PushBindRecord pushBindRecord = new PushBindRecord();
		pushBindRecord.setUserId(userId);
		pushBindRecord.setUserType(bizType);
		BaseApiResult apiResult = new BaseApiResult();
		/*PushBindRecord pushBindRecord = jsonStr2PushBindRecord(pushBindRecordStr);
		if(pushBindRecord == null ){
			apiResult.setCode("0");
			apiResult.setMsg("params Invalid");
		}*/
		try {
			
			List<PushBindRecord> recordList = pushBindRecordService.qureyUserBindByPrefix(pushBindRecord.getUserId(),pushBindRecord.getUserType(),null);

			apiResult.setCode("1");
			apiResult.setData(recordList);
			return apiResult;
			
		} catch (Exception e) {
			
			apiResult.setCode("0");
			apiResult.setMsg("exception");
			
			return apiResult;
		}
	}
		
	
}
